package com.ttc.mBackupK16.BackupPhotos;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import com.ttc.mBackupK16.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RestoreActivity extends Activity {
	private ListView lv;
	private FileItemAdapter fileAdapter;
	private Button btnUnZip;
	private ProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zip_file_list);
		
		lv = (ListView) findViewById(R.id.lv_zip_file_list);
		fileAdapter = new FileItemAdapter();
		fileAdapter.initialize();
		
		lv.setAdapter(fileAdapter);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				Toast.makeText(
						getApplicationContext(), 
						fileAdapter.fileList.get(position).path, 
						Toast.LENGTH_SHORT).show();				
			}
		});
		
	}
	
	/*@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.file_item_cmenu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.cmenu_unzip:
			return true;
		case R.id.cmenu_delete:
			return true;
			default:
				return super.onContextItemSelected(item);
		}
		
	}*/
	private void ShowProgressDialog(){
		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	
	}
	
	private File[] getZipFiles(){
		File rootPath = Environment.getExternalStorageDirectory();
		File imagePath = new File(rootPath,"mBackup2/");
		File [] listFile = imagePath.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String filename) {
				// TODO Auto-generated method stub
				return filename.endsWith(".zip");
			}
		});
		return listFile;
	}

	public class FileItemAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private ArrayList<FileItem> fileList = new ArrayList<FileItem>();
		
		public ArrayList<FileItem> getFileList() {
			return fileList;
		}

		public void setFileList(ArrayList<FileItem> fileList) {
			this.fileList = fileList;
		}

		public void initialize() {
			fileList.clear();
			File[] listFile = getZipFiles();
			if (listFile != null) {
				for (File f : listFile) {
					FileItem item = new FileItem();
					item.fileName = f.getName();
					item.path = f.getPath();
					fileList.add(item);
				}
			}
			setFileList(fileList);
			notifyDataSetChanged();
		}

		public FileItemAdapter() {
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fileList.size();
		}

		@Override
		public FileItem getItem(int index) {
			// TODO Auto-generated method stub
			return fileList.get(index);
		}

		@Override
		public long getItemId(int index) {
			// TODO Auto-generated method stub
			return index;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.zipfileitem, null);
				viewHolder.imageView = (ImageView)convertView.findViewById(R.id.ic_icon);
				
				viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_file_name);
								
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			FileItem item  = getItem(position);
			viewHolder.textView.setId(position);
			viewHolder.textView.setText(item.fileName);
							
			return convertView;
		}

		public class ViewHolder {
			ImageView imageView;
			EditText editText;
			TextView textView;
		}

		public class FileItem {
			String path;
			String fileName;
		}

	}

}
